<template>
  <div>
    <el-dialog
      title="工艺台账"
      :visible.sync="addEditIsDialog_c"
      :close-on-click-modal="false"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="工艺参数：" prop="technologyDeviceParamId">
              <el-select
                v-model="ruleForm.technologyDeviceParamId"
                placeholder="请选择工艺参数"
                clearable
              >
                <el-option
                  v-for="item in paramArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="记录次数：" prop="recordNum">
              <el-input-number
                v-model="ruleForm.recordNum"
                placeholder="请输入记录次数"
                :precision="0"
                :step="1"
                :max="9999999"
                :min="ruleForm.exceedNum"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="超标次数：" prop="exceedNum">
              <el-input-number
                v-model="ruleForm.exceedNum"
                placeholder="请输入超标次数"
                :precision="0"
                :step="1"
                :max="ruleForm.recordNum"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="存在问题：" prop="existProblem">
              <el-input
                v-model="ruleForm.existProblem"
                placeholder="请输入存在问题"
                type="textarea"
                :rows="2"
                maxlength="200"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="改良措施：" prop="amendment">
              <el-input
                v-model="ruleForm.amendment"
                placeholder="请输入改良措施"
                type="textarea"
                :rows="2"
                maxlength="200"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="负责人：" prop="chargePerson">
              <el-input
                v-model="ruleForm.chargePerson"
                placeholder="请输入负责人"
                maxlength="50"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="检查人：" prop="inspectPerson">
              <el-input
                v-model="ruleForm.inspectPerson"
                placeholder="请输入检查人"
                maxlength="50"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { saveDataStandBookTechnology } from "@/api/statistic";
import { getTechnologyDeviceParams } from "@/api/technology";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        technologyDeviceParamId: "",
        exceedNum: "",
        recordNum: "",
        existProblem: "",
        amendment: "",
        chargePerson: "",
        inspectPerson: "",
      },
      rules: {
        // 表单规则
        technologyDeviceParamId: [
          { required: true, message: "请选择工艺参数", trigger: "change" },
        ],
        exceedNum: [
          { required: true, message: "请输入超标次数", trigger: "blur" },
        ],
        recordNum: [
          { required: true, message: "请输入记录次数", trigger: "blur" },
        ],
        existProblem: [
          { required: false, message: "请输入存在问题", trigger: "blur" },
        ],
        amendment: [
          { required: false, message: "请输入改良措施", trigger: "blur" },
        ],
        chargePerson: [
          { required: false, message: "请输入负责人", trigger: "blur" },
        ],
        inspectPerson: [
          { required: false, message: "请输入检查人", trigger: "blur" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      paramArray: [], // 工艺参数数组
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 获取工艺设备参数列表
        this.getTechnologyDeviceParamList();
      }
    },
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveDataStandBookTechnology(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取工艺设备参数列表
    getTechnologyDeviceParamList() {
      getTechnologyDeviceParams({
        id: this.addEditId,
      }).then((res) => {
        if (res.code == 200) {
          this.paramArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}
</style>
